﻿@model SQLInstance
@{
    var jobs = Model.JobSummary;
    var data = jobs.SafeData(true);
}
<h4 class="modal-title" title="Updated @(jobs.LastPoll?.ToRelativeTime())">
    Agent Jobs
</h4>
@if (data.Any())
{
    <table class="table table-striped table-hover text-nowrap table-super-condensed table-responsive">
        <thead>
            <tr>
                <th></th>
                <th>Name</th>
                <th>Last Run</th>
                <th>Start</th>
                <th>End</th>
                <th>Duration</th>
                <th>Enabled</th>
                @if (Current.User.Is(SQLRoles.Admin))
                {
                    <th>Actions</th>
                }
            </tr>
        </thead>
        <tbody>
            @foreach (var j in data.OrderByDescending(j => j.LastRunRequestedDate))
            {
                var lastInitiator = j.LastRunRequestedSource.HasValue ? j.LastRunRequestedSource.Value.AsString(EnumFormat.Description) : "Unknown";
                <tr class="@j.RowClass()@(j.IsRunning ? " info" : null)@(j.IsEnabled ? "" : " text-muted")" data-guid="@j.JobId.ToString()" title="@if(!j.IsEnabled){<text>(DISABLED) 
</text>}Job: @j.Name
@j.Description
@if(j.IsRunning) {<text>Currently Running
</text>}

Category: @j.Category
Created: @j.DateCreated.ToZuluTime()
                            
Last Run Initiated by: @lastInitiator
Last Run Message: @j.LastRunMessage">
                    <td>@j.IconSpan()</td>
                    <td>@j.Name
                        @if (j.IsRunning)
                        {
                            <span class="good">(Running)</span>
                        }
                    </td>
                    @if (j.IsRunning)
                    {
                        <td>Running (@lastInitiator)</td>
                        <td>@(j.LastStartDate?.ToRelativeTimeSpan())</td>
                        <td></td>
                        <td>@(j.LastRunDuration.HasValue ? j.LastRunDuration.Value.ToTimeStringMini() : "")</td>
                    }
                    else if (j.LastRunStatus.HasValue)
                    {
                        <td>@j.LastRunMonitorStatus.IconSpan() @j.LastRunMonitorStatus.Span(j.LastRunStatus.Value.AsString(EnumFormat.Description).IsNullOrEmptyReturn("Unknown")) (@lastInitiator)</td>
                        <td>@(j.LastStartDate?.ToRelativeTimeSpan())</td>
                        <td>@(j.LastStopDate?.ToRelativeTimeSpan())</td>
                        <td>@(j.LastRunDuration.HasValue ? j.LastRunDuration.Value.ToTimeStringMini() : "")</td>
                    }
                    else
                    {
                        <td colspan="4" class="text-muted">no run history found</td>   
                    }
                    <td class="@(j.IsEnabled ? "" : "text-warning")">@(j.IsEnabled ? "Yes" : "No")</td>
                    @if (Current.User.Is(SQLRoles.Admin))
                    {
                        <td class="actions">
                            @if (j.IsEnabled)
                            {
                                <a href="#" class="js-sql-job-action btn btn-xs btn-warning" data-action="toggle" data-enable="false">Disable</a>
                            }
                            else
                            {
                                <a href="#" class="js-sql-job-action btn btn-xs btn-success" data-action="toggle" data-enable="true">Enable</a>
                            }
                            @if (!j.IsRunning)
                            {
                                <a href="#" class="js-sql-job-action btn btn-xs btn-primary" data-action="start" data-guid="@j.JobId.ToString()">Start</a>
                            }
                            else
                            {
                                <a href="#" class="js-sql-job-action btn btn-xs btn-danger" data-action="stop" data-guid="@j.JobId.ToString()">Stop</a>
                            }
                        </td>
                    }
                </tr>
            }
        </tbody>
    </table>
}
else
{
    <div class="no-content">No jobs information available</div>
}